package webBeans;

import java.io.Serializable;
import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;



import beansInterfaces.BasicProductMgr;
import beansInterfaces.ConstraintsMgr;
import beansInterfaces.PackageMgr;
import beansInterfaces.UserMgr;
import dto.ExcursionDTO;
import dto.PackageDTO;
import dto.ResidenceDTO;
import dto.TransportDTO;




@ManagedBean(name = "packageCreation")
@SessionScoped
public class PackageCreationBean  implements Serializable
{
	
	private static final long serialVersionUID = 1L;

	@EJB
	private PackageMgr packageMgr;
	@EJB
	private BasicProductMgr bpMgr;
	@EJB
	private UserMgr u;
	@EJB
	private ConstraintsMgr c;
	
	private PackageDTO currentPackage;
	
	public  PackageCreationBean(){};
	
	@PostConstruct
	public void init()
	{
		this.currentPackage=new PackageDTO();
		float initialValue= 0;
		this.currentPackage.setPrice(initialValue);
		this.currentPackage.setDiscount(0);
		this.currentPackage.setDisponibility(0);
		/*TODO*!*/
	}
	
	//Getters And Setters

	public PackageMgr getPackageMgr() {
		return packageMgr;
	}

	public void setPackageMgr(PackageMgr packageMgr) {
		this.packageMgr = packageMgr;
	}

	public PackageDTO getCurrentPackage() {
		return currentPackage;
	}

	public void setCurrentPackage(PackageDTO currentPackage) {
		this.currentPackage = currentPackage;
	}
	
	public ConstraintsMgr getcMgr() {
		return c;
	}

	//Reset Current
	public void resetCurrentPackage()
	{
		this.currentPackage=new PackageDTO();
	}

	
	//Employee Methods
	
	public String createPackage() 
	{	
		if(!this.isCurrentInDatabase() && !this.c.existsOneNotDisponible(currentPackage))
		{
			this.currentPackage.setAdmin(this.u.getUserDTO().getUsername());
			this.currentPackage.setPrice(this.c.autoSetPrice(this.currentPackage));
			this.currentPackage.setModified(false);
			this.decrementDisponibility();
			this.updateBasicProductsInCurrent();
			this.packageMgr.addPackage(this.currentPackage);
			this.resetCurrentPackage();	
			return "searchPackage";
		}
			return "createPackage";
	}

	public String updatePackage()
	{	
		if(this.isCurrentInDatabase()&& !this.c.existsOneNotDisponible(currentPackage)&& this.c.isComplete(this.currentPackage))
		{
			//this.currentPackage.setPrice(this.c.autoSetPrice(this.currentPackage)); 
			this.decrementDisponibility();
			this.updateBasicProductsInCurrent();
			this.packageMgr.updatePackage(this.currentPackage);
			this.resetCurrentPackage();
			return "searchPackage";
		}
		return "createPackage";
	}
	



		
	

	//Constraints and Utilities

	
	
	public boolean isCurrentInDatabase()
	{
		 Iterator<PackageDTO> iterator = this.packageMgr.getAllPackagesDTO().iterator();
		 
		 while(iterator.hasNext())
		 {	
			 PackageDTO el= iterator.next();
			 if(this.currentPackage.getId()== el.getId())
				 return true;
		 }
		 return false;
	}
	
	public int maxValueForNewPackage()
	{
		
		//max disponibility value for a new package
		
		int x,y,z,w;
		
		if(!this.c.isComplete(this.currentPackage))
			return 0;
		if(this.isCurrentInDatabase())
		{
			x = this.currentPackage.getResidence().getDisponibility() + this.currentPackage.getDisponibility();
			y = this.currentPackage.getExcursion().getDisponibility() + this.currentPackage.getDisponibility();
			z = this.currentPackage.getDepartureTransport().getDisponibility()+ this.currentPackage.getDisponibility();
			w = this.currentPackage.getReturnTransport().getDisponibility()+ this.currentPackage.getDisponibility();
		}
		else
		{
			x = this.currentPackage.getResidence().getDisponibility();
			y = this.currentPackage.getExcursion().getDisponibility();
			z = this.currentPackage.getDepartureTransport().getDisponibility();
			w = this.currentPackage.getReturnTransport().getDisponibility();
		}
		return Math.min(Math.min(x,y), Math.min(z,w));
	}
	
	

   

	private void decrementDisponibility()
	{
		ExcursionDTO exc = this.currentPackage.getExcursion();
		ResidenceDTO res = this.currentPackage.getResidence();
		TransportDTO depTrans= this.currentPackage.getDepartureTransport();
		TransportDTO retTrans= this.currentPackage.getReturnTransport();
		
		int excD=exc.getDisponibility();
		int resD=res.getDisponibility();
		int depTransD=depTrans.getDisponibility();
		int retTransD=retTrans.getDisponibility();
		
		int packD= this.currentPackage.getDisponibility();
		
		this.currentPackage.getExcursion().setDisponibility(excD - packD);
		this.currentPackage.getResidence().setDisponibility(resD - packD);
		this.currentPackage.getDepartureTransport().setDisponibility(depTransD - packD);
		this.currentPackage.getReturnTransport().setDisponibility(retTransD - packD);
	
	
	}

	private void updateBasicProductsInCurrent()
	{
		this.bpMgr.updateExcursion(this.currentPackage.getExcursion());
		this.bpMgr.updateResidence(this.currentPackage.getResidence());
		this.bpMgr.updateTransport(this.currentPackage.getDepartureTransport());
		this.bpMgr.updateTransport(this.currentPackage.getReturnTransport());
	}

	

}
